$(function(){
	var tableSource = new $.jqx.dataAdapter({
		dataType: "json",
		dataFields:[
		    { name: "id", type: "int" },
		    { name: "name", type: "string" },
		    { name: "contacts", type: "string" },
		    { name: "telephone", type: "string" },
		    { name: "cellphone", type: "string" },
		    { name: "logo", type: "string" },
		    { name: "email", type: "string" },
	  	    { name: "status", type: "int" },
		    { name: "province", type: "string" },
		    { name: "city", type: "string" },
		    { name: "district", type: "string" },
		    { name: "street", type: "string" },
		    { name: "detailed_address", type: "string" },
		    { name: "description", type: "string" },
		    { name: "details", type: "string" },
		    { name: "idcard_number", type: "string" },
		    { name: "business_licence", type: "string" },
		    { name: "account_holder", type: "string" },
		    { name: "alipay", type: "string" },
		    { text: "支付宝账号", dataField: "alipay" },
		    { name: "status", type: "int" }
		],
		id: "id",
		url: "delivery/all.do?status=1&where=3"
	});
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: tableSource,
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
                  { text: "序号", width: 50, cellsRenderer: function(row) { return (row + 1).toString(); } },
                  { text: "配送商名称", dataField: "name", width: "10%" },
                  { text: "联系人", dataField: "contacts", width: "8%" },
                  { text: "联系电话", dataField: "telephone", width: "10%", 
                  	cellsRenderer: function(row, column, value, rowData) {
                  		return value ? rowData.cellphone ? value + "/" + rowData.cellphone : value : rowData.cellphone ? rowData.cellphone:"无";
                  	} 
                  },
                  { text: "邮箱", dataField: "email", width: "10%" },
                  { text: "支付宝账号", dataField: "alipay", width: "10%" },
                  { text: "地区", dataField: "district", width: "10%", 
                  	cellsRenderer: function(row, column, value, rowData) {
                  		return rowData.province + "." + rowData.city + "." + rowData.district;
                  	}
                  },
                  { text: "街道", dataField: "street", width: "10%" },
                  { text: "详细地址", dataField: "detailed_address" },
                  { dataField: "idcard_number", hidden: true }
              ],
	});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "查看/修改供应商信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['1000px', '320px'],
		    content: 
		    '<form id="delivery" class="form" method="post" enctype="multipart/form-data" style="height: 100%;overflow: auto;">' +
		    '<input type="hidden" name="id" /><input type="hidden" id="province" name="province" /><input type="hidden" id="city" name="city" /><input type="hidden" id="district" name="district" />' +
				'<input type="hidden" name="submit-url" value="delivery/update.do" />' +
				'<label class="from-label" for="name">供应商名称：</label>' +
				'<input type="text" id="name" name="name" allowBlank="false" class="form-control" placeholder="例：XX供应商" must/>' +
				'<label class="from-label" for="contacts">联系人：</label>' +
				'<input type="text" id="contacts" name="contacts" allowBlank="false" class="form-control" placeholder="例：林总" must="name"/>' +
				'<label class="from-label" for="telephone">座机号码：</label>' +
				'<input type="text" id="telephone" name="telephone" allowBlank="false" class="form-control" placeholder="例：0755-1234567" must="tel"/>' +
				'<label class="from-label" for="cellphone" >手机号码：</label>' +
				'<input type="text" id="cellphone" name="cellphone" allowBlank="false" class="form-control" placeholder="例：13123456789" must="phone"/>' +
				'<label class="from-label" for="idcard_number">身份证：</label>' +
				'<input type="text" id="idcard_number" name="idcard_number" allowBlank="false" class="form-control" placeholder="例：440822222222222222" must="idcard"/>' +
				'<label class="from-label" for="email">E-mail：</label>' +
				'<input type="text" id="email" name="email" allowBlank="false" class="form-control" placeholder="例：11111@qq.com" must="email"/>' +
				'<label class="from-label" for="alipay">支付宝账号：</label>' +
				'<input type="text" id="alipay" name="alipay" allowBlank="false" class="form-control" placeholder="例：13360107777" must/>' +
				'<label class="from-label" for="address">地区：</label>' +
				'<span id="address" allowBlank="false" name="address" placeholder="地址" class="form-control" style="padding:0;"><span id="store-selector"></span></span>' +
				'<label class="from-label" for="street">街道：</label>' +
				'<input type="text" id="street" name="street" class="form-control"  />' +
				'<label class="from-label" for="detailed_address">详细地址：</label>' +
				'<input type="text" id="detailed_address" name="detailed_address" allowBlank="false" class="form-control"  must/>' +
				'<label class="from-label" for="">温馨提示：</label><span style="color:red;">手机号码不能修改，供应商账号为手机号码，密码为：admin123</span>' +
			'</form>' +
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
				'<input type="button" class="btn btn-sm btn-success" onclick="submitClick(this)" value="保存"/>' +
				'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
			'</div>',
		});
	 	setData($("#delivery"), editingData);
	 	$("#address").treemenu({ jsondata: treedata, deep: 2 });
	 	var text = editingData.province + "." + editingData.city + "." + editingData.district;
	 	if(text.indexOf("undefined")==-1){
	 		$("#address").treemenu("setText", text);
	 	}
		var $name =$("#delivery").find("input[name='name']");
	 	if($name.val()!==""&&$name!==undefined){
	 		$name.attr("readonly","readonly");
	 	}else{
	 		$name.removeAttr("readonly");
	 	}
	 	var $cellphone =$("#delivery").find("input[name='cellphone']");
	 	if($cellphone.val()!==""&&$cellphone!==undefined){
	 		$cellphone.attr("readonly","readonly");
	 	}else{
	 		$cellphone.removeAttr("readonly");
	 	}
	 	var $alipay =$("#delivery").find("input[name='alipay']");
	 	if($alipay.val()!==""&&$alipay!==undefined){
	 		$alipay.attr("readonly","readonly");
	 	}else{
	 		$alipay.removeAttr("readonly");
	 	}
	});
});
function submitClick(obj){
	var isOk = JQUtils.form.verifyForm($("#delivery"));
	if (isOk) {
		var text = $("#address").treemenu("getText");
		if (text === undefined || text === null || text === "") {
			layer.tips('请选择地区', '#address', {
				tips : [ 1, '#FF3366' ],
				time : 5000
			});
			return;
		}
		var locationText = text.split(".");
		if (locationText.length < 3) {
			layer.tips('地区选择不正确', '#address', {
				tips : [ 1, '#FF3366' ],
				time : 5000
			});
			return;
		}
			//submitEditer(obj);
		$("#province").val(locationText[0]);
		$("#city").val(locationText[1]);
		$("#district").val(locationText[2]);
		layer.confirm('请确认表单是否已经填写无误？', {
			btn : [ '确认', '取消' ]
		// 按钮
		}, function() {
			var id = $("#supper-editer").find("#id").val();
			$.post("delivery/update.do", $("#delivery")
					.serialize(), function(result) {
				if (result.code == 0||result.code==1) {
					layer.msg(result.msg);
					if (id === "" || id === undefined || id === null) {
						$("#dataTable").jqxDataTable('addRow',
								result.data.id, result.data);
					} else {
						updateRow(null, result.data);
					}
				}
			});
		}, function() {

		});
	}
}

/**
 * 更新行
 * 
 * @param index
 * @param row
 */
function updateRow(index, row) {
	if (index == null) {
		var rows = $("#dataTable").jqxDataTable('getView');
		for (i = rows.length - 1; i >= 0; i--) {
			if (row.uuid === rows[i].uuid) {
				index = i;
			}
		}
	}
	$("#dataTable").jqxDataTable('updateRow', index, row);
	layer.close(_editerIndex);
}
function doSubmit(form, data, url) {
	var address = data.address;
	if(address) {
		var args = address.split(".");
		data.province = args[0];
		data.city = args[1];
		data.district = args[2];
	}
}